using System;
using System.Collections.Generic;
using System.Linq;
using AndyPike.Unfuddle.Commands.Tickets;
using AndyPike.Unfuddle.Entities.Tickets;
using AndyPike.Unfuddle.Exceptions;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;

namespace AndyPike.Unfuddle.Tests.Integration
{
    //[Ignore("Just ignoring integration tests for now to test unit test coverage")]
    public class When_using_the_unfuddle_service_with_tickets : IntegrationTestBase
    {
        [Test]
        public void Should_retrieve_a_list_of_tickets_for_a_project()
        {
            List<Ticket> tickets = unfuddleService.Execute(new FindAllTicketsForProject(projectId));

            Assert.That(tickets, Is.Not.Null);
            Assert.That(tickets.Count, Is.GreaterThan(0));
        }

        [Test]
        public void Should_retrieve_a_single_ticket_for_a_project()
        {
            Ticket ticket = unfuddleService.Execute(new FindTicketById(projectId, ticketId));

            Assert.That(ticket, Is.Not.Null);
            Assert.That(ticket.Id, Is.EqualTo(ticketId));
            Assert.That(ticket.ProjectId, Is.EqualTo(projectId));
        }

        [Test]
        [ExpectedException(typeof(NotFoundRequestException))]
        public void Should_throw_an_exception_if_the_project_id_is_invalid()
        {
            unfuddleService.Execute(new FindTicketById(projectId, -1));
        }

        [Test]
        public void Should_create_a_new_project()
        {
            List<Ticket> ticketsBefore = unfuddleService.Execute(new FindAllTicketsForProject(projectId));

            var ticket = new Ticket
            {
                Summary = "Created ticket integration test",
                ProjectId = projectId,
                Priority = TicketPriority.Low
            };

            unfuddleService.Execute(new CreateTicket(ticket));

            List<Ticket> ticketsAfter = unfuddleService.Execute(new FindAllTicketsForProject(projectId));

            Assert.That(ticketsAfter.Count, Is.GreaterThan(ticketsBefore.Count));
        }

        [Test]
        public void Should_update_a_ticket()
        {
            Guid testId = Guid.NewGuid();

            //Load a ticket from the Unfuddle server and add a Guid to the description
            Ticket ticket = unfuddleService.Execute(new FindTicketById(projectId, ticketId));
            ticket.Description += " -" + testId;

            //Save the ticket
            unfuddleService.Execute(new UpdateTicket(ticket));

            //Load the ticket again and check that the Guid is present
            Ticket savedTicket = unfuddleService.Execute(new FindTicketById(projectId, ticketId));

            Assert.That(savedTicket, Is.Not.Null);
            Assert.That(savedTicket.Id, Is.EqualTo(ticketId));
            Assert.That(savedTicket.Description.Contains(testId.ToString()), Is.True);
        }

        [Test]
        public void Should_delete_an_existing_project()
        {
            var ticket = new Ticket
            {
                Summary = "Delete Ticket Test",
                Description = "This is a description",
                ProjectId = projectId
            };

            unfuddleService.Execute(new CreateTicket(ticket));

            List<Ticket> ticketsBefore = unfuddleService.Execute(new FindAllTicketsForProject(projectId));
            Ticket ticketToDelete = ticketsBefore.Where(t => t.Summary == "Delete Ticket Test").Single();

            unfuddleService.Execute(new DeleteTicket(projectId, ticketToDelete.Id));

            List<Ticket> ticketsAfter = unfuddleService.Execute(new FindAllTicketsForProject(projectId));

            Assert.That(ticketsAfter.Count, Is.EqualTo(ticketsBefore.Count - 1));
        }
    }
}